import 'package:flutter/material.dart';
import 'package:flutter_component_pay/src/theme.dart';

///
/// 第三方支付总金额小部件
///
class AbsThirdPayPriceView extends StatelessWidget {
  /// 总金额
  final double price;

  const AbsThirdPayPriceView({
    super.key,
    required this.price,
  });

  @override
  Widget build(BuildContext context) {
    int asFixed = AbsPayTheme.asFixed;
    List<String> numbers = price.toStringAsFixed(asFixed).split('.');

    return Container(
      padding: const EdgeInsets.only(left: 22, right: 18),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          /// 标题
          Text(AbsPayTheme.subTitle, style: AbsPayTheme.subTitleStyle),

          /// 价格
          RichText(
            textAlign: TextAlign.end,
            text: TextSpan(
              children: [
                WidgetSpan(
                  alignment: PlaceholderAlignment.top,
                  child: Text(
                    '￥',
                    style: TextStyle(
                      color: AbsPayTheme.priceColor,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                WidgetSpan(
                  alignment: PlaceholderAlignment.middle,
                  child: Text(
                    numbers.isNotEmpty ? numbers[0] : '$price',
                    style: TextStyle(
                      color: AbsPayTheme.priceColor,
                      fontSize: 25,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                if (numbers.length > 1)
                  WidgetSpan(
                    alignment: PlaceholderAlignment.top,
                    child: Text(
                      '.',
                      style: TextStyle(
                        color: AbsPayTheme.priceColor,
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                if (numbers.length > 1)
                  WidgetSpan(
                    alignment: PlaceholderAlignment.top,
                    child: Row(
                      children: numbers.asMap().entries.map((element) {
                        if (element.key == 0) {
                          return const SizedBox.shrink();
                        } else {
                          return Text(
                            element.value,
                            style: TextStyle(
                              color: AbsPayTheme.priceColor,
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                            ),
                          );
                        }
                      }).toList(),
                    ),
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
